﻿using System;
using System.Collections.Generic;
using System.Web;
using Microsoft.Data;
using System.Dynamic;
using HtmlExtensions;
using System.Linq;
/// <summary>
/// Summary description for ClassName
/// </summary>
public class MeetingRepository
{
    
    public Meeting Create(dynamic meeting)
    {        
        var db=DB.Open();
        db.Execute("insert into meeting (Date,Topic,Abstract,SpeakerName,SpeakerUrl,SpeakerBio) values(@0,@1,@2,@3,@4,@5)",meeting.Date,meeting.Topic,meeting.Abstract,meeting.SpeakerName,meeting.SpeakerUrl,meeting.SpeakerBio);
        meeting.Id=db.GetLastInsertId();
        return AsMeeting(meeting);
        
    }
    public void Save(dynamic meeting)
    {        
        DB.Open().Execute("update meeting set Date=@0,Topic=@1,Abstract=@2,SpeakerName=@3,SpeakerUrl=@4,SpeakerBio=@5 where id=@6",meeting.Date,meeting.Topic,meeting.Abstract,meeting.SpeakerName,meeting.SpeakerUrl,meeting.SpeakerBio,meeting.Id);                
    }
    public Meeting New()
    {
        Meeting meeting = new Meeting();
        meeting.Date = DateTime.Now.ToShortDateString(); 
        meeting.Topic="";
        meeting.Abstract="";
        meeting.SpeakerName="";
        meeting.SpeakerUrl="";
        meeting.SpeakerBio="";
        return meeting;
    }
    
    public Meeting GetNextMeeting(){
        return GetNextMeeting(DateTime.Now.AddDays(-1));
    }
    public Meeting GetNextMeeting(DateTime date)
    {
        var query ="select Top(1) * from Meeting where Date>=@0 Order by Date";
        return AsMeeting(DB.Open().QuerySingle(query,date));   
    }
    public IEnumerable<Meeting> GetFutureMeetings(){
        return GetFutureMeetings(DateTime.Now);
    }

    public IEnumerable<Meeting> GetFutureMeetings(DateTime date){
        var query ="select * from Meeting where Date>=@0 Order by Date";
        return DB.Open().Query(query,date).Select(row=>AsMeeting(row) as Meeting).ToArray();
    }
        
    public IEnumerable<Meeting> GetPreviousMeetings(){
        return GetPreviousMeetings(DateTime.Now);
    }

    public IEnumerable<Meeting> GetPreviousMeetings(DateTime date){
        var query ="select * from Meeting where Date<@0 Order by Date desc";
        return DB.Open().Query(query,date).Select(row=>AsMeeting(row) as Meeting).ToArray();
    }
    public Meeting GetMeeting(string id){        
        var query ="select * from Meeting where Id=@0";
        return AsMeeting(DB.Open().QuerySingle(query,id));
    }
    public IEnumerable<Meeting> GetMeetings()
    {
        var query ="select * from Meeting order by Date";        
        return DB.Open().Query(query).Select(row => AsMeeting(row) as Meeting).ToArray();
    }    
    public  Meeting AsMeeting(dynamic input){
        if(input!=null) {   
            Meeting output = new Meeting();
            output.Id=input.Id;
            output.SpeakerName=input.SpeakerName;
            output.SpeakerUrl=input.SpeakerUrl;
            output.SpeakerBio=input.SpeakerBio;
            output.Topic=input.Topic;
            output.Abstract=input.Abstract;
            output.Date=input.Date.ToShortDateString();
            return output;
        }
        return null;
    }
}

public class DB{
    public static Database Open()
    {
        return Database.OpenFile("adnug.sdf");
    }
}